package com.attendance.service.impl;

import com.attendance.dto.request.AttRuleRequest;
import com.attendance.entity.AttRule;
import com.attendance.common.BusinessException;
import com.attendance.mapper.AttRuleMapper;
import com.attendance.service.AttRuleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class AttRuleServiceImpl extends ServiceImpl<AttRuleMapper, AttRule> implements AttRuleService {

    @Override
    public void createRule(AttRuleRequest request) {
        // 检查规则名称是否重复
        if (isRuleNameExists(request.getRuleName(), null)) {
            throw new BusinessException("规则名称已存在");
        }
        
        AttRule rule = new AttRule();
        BeanUtils.copyProperties(request, rule);
        save(rule);
    }

    @Override
    public void updateRule(AttRuleRequest request) {
        // 检查规则是否存在
        AttRule existingRule = getById(request.getId());
        if (existingRule == null) {
            throw new BusinessException("考勤规则不存在");
        }
        
        // 检查规则名称是否重复
        if (isRuleNameExists(request.getRuleName(), request.getId())) {
            throw new BusinessException("规则名称已存在");
        }
        
        BeanUtils.copyProperties(request, existingRule);
        updateById(existingRule);
    }

    @Override
    public void deleteRule(Long id) {
        if (!removeById(id)) {
            throw new BusinessException("考勤规则不存在");
        }
    }

    @Override
    public AttRule getRule(Long id) {
        AttRule rule = getById(id);
        if (rule == null) {
            throw new BusinessException("考勤规则不存在");
        }
        return rule;
    }

    @Override
    public Page<AttRule> pageRules(Integer pageNum, Integer pageSize, String ruleName) {
        LambdaQueryWrapper<AttRule> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(ruleName)) {
            wrapper.like(AttRule::getRuleName, ruleName);
        }
        wrapper.orderByDesc(AttRule::getCreateTime);
        
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        AttRule rule = getById(id);
        if (rule == null) {
            throw new BusinessException("考勤规则不存在");
        }
        
        rule.setStatus(status);
        updateById(rule);
    }
    
    /**
     * 检查规则名称是否重复
     * @param ruleName 规则名称
     * @param excludeId 排除的ID（用于更新时）
     * @return true-重复，false-不重复
     */
    private boolean isRuleNameExists(String ruleName, Long excludeId) {
        LambdaQueryWrapper<AttRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttRule::getRuleName, ruleName);
        if (excludeId != null) {
            wrapper.ne(AttRule::getId, excludeId);
        }
        return count(wrapper) > 0;
    }
} 